package com.chenexample.myshop16173;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Toast;

import com.chenexample.myshop16173.adapter.MyRVAdapter1;
import com.chenexample.myshop16173.fragment.HomeFragment;
import com.chenexample.myshop16173.util.GetImage;
import com.chenexample.myshop16173.util.util;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExchangeActivity extends FragmentActivity {
    RecyclerView recyclerView;
    MyRVAdapter1 adapter;
    List<Exchanfe> newList = new ArrayList<>();
    private boolean first;
    private boolean canRefresh;
    private LoginAC ac;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exchange);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        first = true;
        canRefresh = true;
        ac = new LoginAC();
    }


    class LoginAC extends AsyncTask<Integer, Object, Object> {

        private ProgressDialog pd;
        private String imgUrlHead;

        @Override
        protected Object doInBackground(Integer... params) {
            int res = 0;
            imgUrlHead = "http://redcomet.top/myshop/images/goods";
            String uri = "http://redcomet.top/myshop/goods/new_goodsForMobile.action";
            try {
                JSONArray jsonArray = new JSONArray(String.valueOf(util.getHttpJsonByhttpclient(uri)));
                String status = null;
                String msg = null;
                List<Map<String, String>> list = null;
                if (jsonArray.length() > 1) {
                    list = new ArrayList<>();
                }
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject2 = (JSONObject) jsonArray.opt(i);
                    if (i == 0) {
                        status = jsonObject2.getString("status");
                        msg = jsonObject2.getString("msg");
                    } else {
                        Map<String, String> map = new HashMap<>();
                        map.put("good_name", jsonObject2.getString("good_name"));
                        map.put("good_price", jsonObject2.getString("good_price"));
                        map.put("good_over", jsonObject2.getString("good_over"));
                        map.put("good_id", jsonObject2.getString("good_id"));
                        list.add(map);
                    }
                }
                if (status.equals("ok") && list != null) {
                    res++;
                    Map<String, Object> mapTemp = null;
                    for (Map<String, String> map : list) {
                        mapTemp = new HashMap<>();
                        Exchanfe exchanfe = new Exchanfe();
                        exchanfe.setGood_id(map.get("good_id"));
                        exchanfe.setGood_name(map.get("good_name"));
                        exchanfe.setGood_over(map.get("good_over"));
                        exchanfe.setGood_price(map.get("good_price"));
                        exchanfe.setImage(imgUrlHead + "/" + map.get("good_id") + "_01.jpg");
                        mapTemp.put("imgBit", GetImage.getInternetPicture(ExchangeActivity.this, imgUrlHead + "/" + map.get("good_id") + "_01.jpg"));
                        newList.add(exchanfe);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            adapter=new MyRVAdapter1(ExchangeActivity.this,newList);
                            recyclerView.setAdapter(adapter);
                            Log.e("aa", newList.toString() + "");
                            recyclerView.setItemAnimator(new DefaultItemAnimator());
                            recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(ExchangeActivity.this).build());
                            recyclerView.setLayoutManager(new GridLayoutManager(ExchangeActivity.this, 2));
                        }
                    });

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }


            return res;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pd = new ProgressDialog(ExchangeActivity.this);
            pd.setMessage("加载中...");
            if (!pd.isShowing()) {
                pd.show();
            }

        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int res = (int) o;
            if (res != 0) {
                if (res != 3) {
                    Toast.makeText(ExchangeActivity.this, "加载失败！", Toast.LENGTH_LONG);
                } else {
                    Toast.makeText(ExchangeActivity.this, "加载成功", Toast.LENGTH_LONG);
                }
            } else {
                Toast.makeText(ExchangeActivity.this, "加载完成！", Toast.LENGTH_LONG);
            }
            pd.dismiss();
            canRefresh = true;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        System.out.println("---------------------------开始了----------------------------------------");
        if (first && canRefresh) {
            canRefresh = false;
            try {
                ac.execute(123);
                first = false;
            } catch (Exception e) {
                System.out.println("这行了二次homeAC");
            }
        }
    }
}
